package com.sctlqq.wuye.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sctlqq.wuye.entity.*;
import com.sctlqq.wuye.service.GoodsService;
import com.sctlqq.wuye.service.IntegralService;
import com.sctlqq.wuye.service.RefuseClassificationService;
import com.sctlqq.wuye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分商城兑换商品记录的  前端控制器
 */
@RestController
@RequestMapping("/sctlqq/api/wuye/integral")
public class IntegralController {

    @Autowired//自动实例化
    private IntegralService integralService;//兑换商品  服务层对象

    @Autowired
    private UserService userService;//用户  服务层对象

    @Autowired
    private GoodsService goodsService;//积分商品  服务层对象

    /**
     * 根据业主uid 获取积分兑换历史列表
     * @param uid
     * @return
     */
    @PostMapping("/getlistbyuid")
    public Result<Object> getListByUid(@RequestBody Long uid){
        return integralService.getIntegralDetailList(uid);
    }

    /**
     * 新增 积分兑换明细
     * @param integral
     * @return
     */
    @PutMapping("/addIntegralDetail")
    public Result<Object> addIntegralDetail(@RequestBody Integral integral){
        List<Map> mapList=new ArrayList<>();
        Map<String,Boolean> map =new HashMap<>();
        Map<String,User> userMap =new HashMap<>();
        //获取业主信息对象
        User user=userService.getById(integral.getUid());
        //获取商品信息对象
        Goods goods=goodsService.getById(integral.getGid());
        //修改业主总积分
        user.setSumValue(user.getSumValue()-goods.getPrice());
        //更新业主信息
        boolean ok1=userService.updateById(user);
        //新增积分明细
        boolean ok2=integralService.save(integral);
        map.put("isok",ok1 && ok2);
        userMap.put("userInfo",user);
        mapList.add(map);
        mapList.add(userMap);
        return new Result<>(HttpStatus.OK.value(),"success",mapList);
    }


}
